package vashion.azeroth.core.activity;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import vashion.azeroth.core.pojo.AttributeUtils;
import vashion.azeroth.core.pojo.BaseObject;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 描述：全球购活动记录表
 * Date 2014-05-28
 */
@Data
public class GlobalActivityInfoDO extends BaseObject {

    private static final long serialVersionUID = -7370193985418336573L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 创建时间
     */
    private Date gmtCreate;

    /**
     * 修改时间
     */
    private Date gmtModified;

    /**
     * 标题
     */
    private String title;

    /**
     * 活动状态。  1：生效  0：编辑中
     */
    private Integer status;

    /**
     * 小二ID
     */
    private Long adminId;

    /**
     * 小二nick
     */
    private String adminNick;

    /**
     * 活动开始时间
     */
    private Date startTime;

    /**
     * 活动结束时间
     */
    private Date endTime;

    /**
     * 活动类型 见：ActiveSchemaEnum
     */
    private int activityType;

    /**
     * 活动维度 见：ActiveContentUnitType
     */
    private int unitType;

    /**
     * 存放数据格式定义
     */
    private String schemaInfo;

    /**
     * 备注
     */
    private String attributes;

    public Map<String, String> parseAttributes() {
        return AttributeUtils.getAttributeKeyToValueMap(attributes);
    }

    public Map<String, String> parseSchemaKeyToValue() {
        return AttributeUtils.getAttributeKeyToValueMap(schemaInfo);
    }

    public Map<String, String> parseSchemaValueToKey() {
        return AttributeUtils.getAttributeValueToKeyMap(schemaInfo);
    }

    public Map<String, String> parseContent(Map<String/**key*/, String/**value*/> schemaKeyToValueMap) {
        JSONObject json = (JSONObject) JSONObject.parse(attributes);
        JSONObject jsonContent = json.getJSONObject(ActiveConstant.JSON_KEY);
        Map<String, String> contentMap = new LinkedHashMap<String, String>();
        for (String key : schemaKeyToValueMap.keySet()) {
            String valueContent = jsonContent.getString(key);
            contentMap.put(key, valueContent == null ? "" : valueContent.trim());
        }
        return contentMap;
    }

    public String getContentAttribute(String key) {
        try {
            String content = this.getAttributes();
            if (content != null && !content.trim().isEmpty()) {
                JSONObject json = (JSONObject) JSONObject.parse(content);
                JSONObject jsonContent = json.getJSONObject(ActiveConstant.JSON_KEY);
                return jsonContent.get(key).toString();
            }
        } catch (Exception e) {
        }
        return null;
    }
}